package GameElements;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.SoundManager;
import com.thauanLopes.bacterium.GameManages.CashManager;
import com.thauanLopes.smagasmaga.R;

public class Coin extends GameObject {

	private final String TAG = "Coin";

	private Bitmap bitmap;
	private Rect rect;

	private float lifeTime = 2;

	private float lifeCronometer = 0;

	private List<GameObject> gameObjects;
	private List<GameObject> cemetery;

	private int x;
	private int y;

	public Coin(List<GameObject> gameObjects, List<GameObject> cemetery) {
		this.gameObjects = gameObjects;
		this.cemetery = cemetery;
		//this.bitmap = BitmapStorage.getInstance().getCoin();

		int tmpX = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasWidth() - bitmap.getWidth() - 2
						* (MinhasCoisas.getCanvasWidth() / 10))
				+ (MinhasCoisas.getCanvasWidth() / 10);
		int tmpY = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasHeight() - bitmap.getHeight() - 2
						* (MinhasCoisas.getCanvasHeight() / 10))
				+ MinhasCoisas.getCanvasHeight() / 10;

		this.x = tmpX;
		this.y = tmpY;

		this.rect = new Rect(x, y, x + bitmap.getWidth(), y
				+ bitmap.getHeight());

		this.gameObjects.add(this);
	}

	@Override
	public void Update(double d) {

		if (this.lifeCronometer < this.lifeTime) {
			this.lifeCronometer += d;
		} else {
			Die();

		}
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		float fingerX = event.getRawX();
		float fingerY = event.getRawY();

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			if (this.rect.contains((int) fingerX, (int) fingerY)) {
				Log.i(TAG, "Coin!");
				Cash();
				Die();

				return true;
			}
			break;
		}
		return false;
	}

	@Override
	public void Draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, this.x, this.y, MinhasCoisas.getPaint());
	}

	@Override
	public void Die() {
		this.cemetery.add(this);
	}

	public void Cash() {
		SoundManager.getInstance().playSound(R.raw.coin);
		CashManager.getInstance().AddCash(1);
	}

	@Override
	public void Destroy() {
		// Adeus!
		gameObjects.remove(this);
	}

}
